package com.spring.demo.springdemo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author RenPu
 * @Date 2022/4/10 14:11
 * @Version 1.0
 * @Description:
 **/


/**
 * 低价榜
 */
@Slf4j
@Service("LowPrice")
public class LowPriceRank implements Strategy {
    @Override
    public List<Stock> sort(List<Stock> source) {

        List<Stock> stockList=source.stream()
                .sorted(Comparator.comparing(Stock::getPrice))
                .collect(Collectors.toList());
        log.info("LowPrice处理的结果集: {}",stockList);
        return stockList;
    }


    /**
     * 使用注解得形式，实现事件监听机制
     * @param applicationEvent
     */
    @EventListener
    public void listerner(ApplicationEvent applicationEvent){
        PayloadApplicationEvent applicationEvent1=  (PayloadApplicationEvent)applicationEvent;
        System.out.println(applicationEvent1);
    }


}
